package cn.com.surker.material.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.enums.material.MaterialBreedTypeEnum;
import cn.com.surker.excel.ExcelReportUtils;
import cn.com.surker.material.service.MaterialReportBaseService;
import cn.com.surker.material.vo.MainMaterialWeightVo;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.main.MainMaterialWeightVO;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.*;

import static cn.com.surker.excel.ExcelReportUtils.exportTotalReport;

/**
 * @Author lucky
 * @create 2024年11月26 16:11
 * @desc
 */
public abstract class MaterialReportBaseServiceImpl implements MaterialReportBaseService {

    public XSSFWorkbook exportStorageWeight(String sheetName, String title, List<MainMaterialWeightVo> weightList) {
        if (weightList == null || weightList.size() == 0)
            throw new MessageCenterException("没有任何导出数据~！");
        Map<Integer, String> breedMap = new HashMap<>();
        for (int i = 0; i < MaterialBreedTypeEnum.values().length; i++) {
            breedMap.put(MaterialBreedTypeEnum.values()[i].getCode(), MaterialBreedTypeEnum.values()[i].getDescription());
        }
        XSSFWorkbook hw = new XSSFWorkbook();
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        for (int i = 0; i < weightList.size(); i++) {
            MainMaterialWeightVo weight = weightList.get(i);
            String isEnjoy = "共享仓";
            String storageType = "筒仓";
            String breedType = "";
            Map<String, Object> temp = new LinkedHashMap<>();
            if (!StringUtils.isNull(weight.getIsEnjoy()) && weight.getIsEnjoy() == 1)
                isEnjoy = "非共享仓";
            if (!StringUtils.isNull(weight.getStorageType())) {
                if (weight.getStorageType() == 1)
                    storageType = "堆仓";
                else
                    storageType = "无类型";
            }
            Double supplierNetWeight = 0.0;
            Double netWeight = 0.0;
            Integer tranNo = weight.getCarTotal();//车次
            if (!StringUtils.isNull(weight.getNetWeight()))
                netWeight = StringUtils.formatDouble(weight.getNetWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getSupplierNetWeight()))
                supplierNetWeight = StringUtils.formatDouble(weight.getSupplierNetWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getBreedType()) && !StringUtils.isNull(breedMap.get(weight.getBreedType())))
                breedType = StringUtils.convertString(breedMap.get(weight.getBreedType()));
            temp.put("supplier", weight.getSupplier());
            temp.put("material", weight.getMaterial());
            temp.put("storage", weight.getStorage());
            temp.put("netWeight", netWeight);
            temp.put("supplierNetWeight", supplierNetWeight);
            temp.put("tranNo", tranNo);
            temp.put("isEnjoy", isEnjoy);
            temp.put("storageType", storageType);
            temp.put("breedType", breedType);
            temp.put("inventoryTime", weight.getInventoryTime());
//            temp.put("taxCode", weight.getSupplierCode());
            list.add(temp);
        }
        Object[] columns = {"供应商","材料","仓位","净重(吨)","供方净重(吨)","车次(车)","是否共享仓","仓位类型","材料品种","盘存时间"};
        keyList.add("tranNo");
        keyList.add("netWeight");
        keyList.add("supplierNetWeight");
        hw = exportTotalReport(sheetName, title, columns, keyList, list, hw);
        return hw;
    }
    /**
     * 导出原材购进明细报表
     * @param sheetName
     * @param weightList
     * @return
     */
    public XSSFWorkbook exportTotalSupplier(String sheetName, String title, List<MainMaterialWeightVO> weightList) {
        if (weightList == null || weightList.size() == 0)
            throw new MessageCenterException("没有任何导出数据~！");
        XSSFWorkbook hw = new XSSFWorkbook();
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        for (int i = 0; i < weightList.size(); i++) {
            MainMaterialWeightVO weight = weightList.get(i);
            String isAuto = "自动";
            Map<String, Object> temp = new LinkedHashMap<>();
            if (!StringUtils.isNull(weight.getIsAuto()) && weight.getIsAuto() == 1)
                isAuto = "手动";
            String inTime = "";
            String outTime = "";
            Double realNetWeight = 0.0;//真实净重
            Double supplierNetWeight = 0.0;
            Double netWeight = 0.0;
            Integer tranNo = weight.getTranNo();//车次
            Double differWeight = 0.0;//量差 供方净重 - 净重
            if (!StringUtils.isNull(weight.getRealNetWeight()))
                realNetWeight = StringUtils.formatDouble(weight.getRealNetWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getNetWeight()))
                netWeight = StringUtils.formatDouble(weight.getNetWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getSupplierNetWeight()))
                supplierNetWeight = StringUtils.formatDouble(weight.getSupplierNetWeight() / 1000, 2);
            differWeight = supplierNetWeight - netWeight;
            temp.put("supplier", weight.getSupplier());
            temp.put("material", weight.getMaterial());
            temp.put("realNetWeight", realNetWeight);
            temp.put("netWeight", netWeight);
            temp.put("supplierNetWeight", supplierNetWeight);
            temp.put("tranNo", tranNo);
            temp.put("differWeight", differWeight);
//            temp.put("taxCode", weight.getSupplierCode());
            list.add(temp);
        }
        Object[] columns = {"供应商","材料","真实净重(吨)","打印净重(吨)","供方净重(吨)","车次(车)","量差(吨)"};
        keyList.add("realNetWeight");
        keyList.add("netWeight");
        keyList.add("supplierNetWeight");
        keyList.add("tranNo");
        keyList.add("differWeight");
        hw = exportTotalReport(sheetName, title, columns, keyList, list, hw);
        return hw;
    }

    /**
     * 导出原材购进明细报表
     * @param sheetName
     * @param weightList
     * @return
     */
    public XSSFWorkbook exportMaterialDetail(String sheetName, String title, List<MainMaterialWeightVO> weightList) {
        if (weightList == null || weightList.size() == 0)
            throw new MessageCenterException("没有任何导出数据~！");
        XSSFWorkbook hw = new XSSFWorkbook();
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> keyList = new ArrayList<>();
        for (int i = 0; i < weightList.size(); i++) {
            MainMaterialWeightVO weight = weightList.get(i);
            String isAuto = "自动";
            Map<String, Object> temp = new LinkedHashMap<>();
            if (!StringUtils.isNull(weight.getIsAuto()) && weight.getIsAuto() == 1)
                isAuto = "手动";
            String inTime = "";
            String outTime = "";
            Double realGross = 0.0;
            Double realNetWeight = 0.0;//真实净重
            Double hiddenWeight = 0.0;//暗扣
            Double brightWeight = 0.0;//明扣
            Double deductWeight = 0.0;//扣重
            Double gross = 0.0;
            Double netWeight = 0.0;
            Double tare = 0.0;
            String isCheck = "未审核";
            String isStore = "不计算";
            String isBill = "不结算";
            Double supplierNetWeight = 0.0;//供方净重
            Double differWeight = 0.0;//量差 供方净重 - 净重
            if (!StringUtils.isNull(weight.getInTime()))
                inTime = DateUtil.convertString(weight.getInTime());
            if (!StringUtils.isNull(weight.getOutTime()))
                outTime = DateUtil.convertString(weight.getOutTime());
            if (!StringUtils.isNull(weight.getRealGross()))
                realGross = StringUtils.formatDouble(weight.getRealGross() / 1000, 2);
            if (!StringUtils.isNull(weight.getRealNetWeight()))
                realNetWeight = StringUtils.formatDouble(weight.getRealNetWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getHiddenWeight()))
                hiddenWeight = StringUtils.formatDouble(weight.getHiddenWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getBrightWeight()))
                brightWeight = StringUtils.formatDouble(weight.getBrightWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getIsCheck()) && weight.getIsCheck() == 0)
                isCheck = "已审核";
            if (!StringUtils.isNull(weight.getIsStore()) && weight.getIsStore() == 0)
                isStore = "计算";
            if (!StringUtils.isNull(weight.getIsBill()) && weight.getIsBill() == 0)
                isBill = "结算";
            deductWeight = hiddenWeight + brightWeight;
            if (!StringUtils.isNull(weight.getGross()))
                gross = StringUtils.formatDouble(weight.getGross() / 1000, 2);
            if (!StringUtils.isNull(weight.getNetWeight()))
                netWeight = StringUtils.formatDouble(weight.getNetWeight() / 1000, 2);
            if (!StringUtils.isNull(weight.getTare()))
                tare = StringUtils.formatDouble(weight.getTare() / 1000, 2);
            if (!StringUtils.isNull(weight.getSupplierNetWeight()))
                supplierNetWeight = StringUtils.formatDouble(weight.getSupplierNetWeight() / 1000, 2);
            differWeight = supplierNetWeight - netWeight;
            temp.put("code", weight.getCode());
            temp.put("isAuto", isAuto);
            temp.put("supplier", weight.getSupplier());
            temp.put("material", weight.getMaterial());
            temp.put("storage", weight.getStorage());
            temp.put("carNo", weight.getCarNo());
            temp.put("inTime", inTime);
            temp.put("outTime", outTime);
            temp.put("gross", gross);
            temp.put("tare", tare);
            temp.put("brightWeight", brightWeight);
            temp.put("netWeight", netWeight);
            temp.put("supplierNetWeight", supplierNetWeight);
            temp.put("differWeight", differWeight);
            temp.put("isCheck", isCheck);
            temp.put("creator", weight.getCreator());
            temp.put("isStore", isStore);
            temp.put("isBill", isBill);
            temp.put("realGross", realGross);
            temp.put("realNetWeight", realNetWeight);
            temp.put("hiddenWeight", hiddenWeight);
            temp.put("deductWeight", deductWeight);
            temp.put("supplierOrderId", weight.getSupplierOrderId());
//            temp.put("taxCode", weight.getSupplierCode());
            temp.put("remark", weight.getRemark());
            list.add(temp);
        }
        Object[] columns = {"磅单号","过磅方式","供应商","材料名称","仓位","车牌","入场时间","出场时间",
                "毛重","皮重","明扣","净重","供方净重","量差","是否审核","操作人","计算库存","是否结算","真实毛重","真实净重","暗扣", "扣重",
                "供方单号","备注"};
        keyList.add("realGross");
        keyList.add("realNetWeight");
        keyList.add("hiddenWeight");
        keyList.add("brightWeight");
        keyList.add("deductWeight");
        keyList.add("gross");
        keyList.add("netWeight");
        keyList.add("tare");
        keyList.add("supplierNetWeight");
        keyList.add("differWeight");
        hw = exportTotalReport(sheetName, title, columns, keyList, list, hw);
        return hw;
    }

}
